/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.flipkart.ohos.proteus;

import ohos.aafwk.ability.AbilityPackage;

import com.flipkart.ohos.proteus.api.ProteusManager;
import com.flipkart.ohos.proteus.converter.GsonConverterFactory;
import com.flipkart.ohos.proteus.gson.ProteusTypeAdapterFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import retrofit2.Retrofit;

public class DemoApplication extends AbilityPackage {
    private Gson gson;
    private static ProteusManager proteusManager;
    private Retrofit retrofit;
    private static final String BASE_URL = "http://10.0.2.2:8080/data/";

    @Override
    public void onInitialize() {
        super.onInitialize();
        // register the proteus type adapter to deserialize proteus resources
        ProteusTypeAdapterFactory adapter = new ProteusTypeAdapterFactory(this);
        gson = new GsonBuilder()
            .registerTypeAdapterFactory(adapter)
            .create();
        // add gson to retrofit to allow deserializing proteus resources when fetched via retrofit
        retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(getGson()))
            .build();

        proteusManager = new ProteusManager(getRetrofit());
        // add gson to retrofit to allow deserializing proteus resources when fetched via retrofit
    }

    public Gson getGson() {
        return gson;
    }

    public static ProteusManager getProteusManager() {
        return proteusManager;
    }

    public Retrofit getRetrofit() {
        return retrofit;
    }
}
